from django.test import TestCase
from django.test.client import Client

"""
Testira stranu sa podacima o igracu
"""
class UserTestCase(TestCase):

    def setUp(self):
        pass

    def runTest(self):
        self.runTestPlayer2()
        self.runTestPlayer3()
        self.runTestPlayer5()
    
    def runTestPlayer2(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/user/2/")
        self.failIf(response == None)
        self.failIf(response.context == None)
        self.failUnlessEqual(len(response.context), 2)

        self.failIf(response.context[0]["user_object"] == None)
        user = response.context[0]["user_object"]        
        self.failUnlessEqual(user.id, 2)
        self.failUnlessEqual(user.chess_points, 1184)
        self.failUnlessEqual(user.tf_points, 1200)
        
        self.failIf(response.context[0]["point_logs"] == None)
        point_logs = response.context[0]["point_logs"]
        self.failUnlessEqual(len(point_logs), 1)
        self.failUnlessEqual(point_logs[0][0].id, 1)
        self.failUnlessEqual(point_logs[0][1], None)

        # chess stuff
        self.failIf(response.context[0]["chess"]["last_match"] == None)
        last_match = response.context[0]["chess"]["last_match"]
        self.failUnlessEqual(last_match.id, 1)
        
        self.failUnlessEqual(response.context[0]["chess"]["standing"], 12)
        self.failUnlessEqual(response.context[0]["chess"]["matches_played"], 1)
        self.failUnlessEqual(response.context[0]["chess"]["matches_won"], 0)
        self.failUnlessEqual(response.context[0]["chess"]["matches_lost"], 1)
        
        max_points_log = response.context[0]["chess"]["max_points_log"]
        self.failUnlessEqual(1200, max_points_log[0])

        min_points_log = response.context[0]["chess"]["min_points_log"]
        self.failUnlessEqual(1184, min_points_log[0])

        # table football stuff
        self.failIf(response.context[0]["tf"]["last_match"] != None)
        
        self.failUnlessEqual(response.context[0]["tf"]["standing"], 5)
        self.failUnlessEqual(response.context[0]["tf"]["matches_played"], 0)
        self.failUnlessEqual(response.context[0]["tf"]["matches_won"], 0)
        self.failUnlessEqual(response.context[0]["tf"]["matches_lost"], 0)
        
        max_points_log = response.context[0]["tf"]["max_points_log"]
        self.failUnlessEqual(1200, max_points_log[0])

        min_points_log = response.context[0]["tf"]["min_points_log"]
        self.failUnlessEqual(1200, min_points_log[0])
        
    def runTestPlayer3(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/user/3/")
        self.failIf(response == None)
        self.failIf(response.context == None)
        self.failUnlessEqual(len(response.context), 2)

        self.failIf(response.context[0]["user_object"] == None)
        user = response.context[0]["user_object"]        
        self.failUnlessEqual(user.id, 3)
        self.failUnlessEqual(user.chess_points, 1199)
        self.failUnlessEqual(user.tf_points, 1200)
        
        self.failIf(response.context[0]["point_logs"] == None)
        point_logs = response.context[0]["point_logs"]
        self.failUnlessEqual(len(point_logs), 2)
        self.failUnlessEqual(point_logs[0][0].id, 4)
        self.failUnlessEqual(point_logs[0][1], None)
        self.failUnlessEqual(point_logs[1][0].id, 2)
        self.failUnlessEqual(point_logs[1][1].id, 4)

        # chess stuff
        self.failIf(response.context[0]["chess"]["last_match"] == None)
        last_match = response.context[0]["chess"]["last_match"]
        self.failUnlessEqual(last_match.id, 2)
        
        self.failUnlessEqual(response.context[0]["chess"]["standing"], 11)
        self.failUnlessEqual(response.context[0]["chess"]["matches_played"], 2)
        self.failUnlessEqual(response.context[0]["chess"]["matches_won"], 1)
        self.failUnlessEqual(response.context[0]["chess"]["matches_lost"], 1)
        
        max_points_log = response.context[0]["chess"]["max_points_log"]
        self.failUnlessEqual(1216, max_points_log[0])

        min_points_log = response.context[0]["chess"]["min_points_log"]
        self.failUnlessEqual(1199, min_points_log[0])

        # table football stuff
        self.failIf(response.context[0]["tf"]["last_match"] != None)
        
        self.failUnlessEqual(response.context[0]["tf"]["standing"], 5)
        self.failUnlessEqual(response.context[0]["tf"]["matches_played"], 0)
        self.failUnlessEqual(response.context[0]["tf"]["matches_won"], 0)
        self.failUnlessEqual(response.context[0]["tf"]["matches_lost"], 0)
        
        max_points_log = response.context[0]["tf"]["max_points_log"]
        self.failUnlessEqual(1200, max_points_log[0])

        min_points_log = response.context[0]["tf"]["min_points_log"]
        self.failUnlessEqual(1200, min_points_log[0])

    def runTestPlayer5(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/user/5/")
        self.failIf(response == None)
        self.failIf(response.context == None)
        self.failUnlessEqual(len(response.context), 2)

        self.failIf(response.context[0]["user_object"] == None)
        user = response.context[0]["user_object"]        
        self.failUnlessEqual(user.id, 5)
        self.failUnlessEqual(user.chess_points, 1200)
        self.failUnlessEqual(user.tf_points, 1260)
        
        self.failIf(response.context[0]["point_logs"] == None)
        point_logs = response.context[0]["point_logs"]
        self.failUnlessEqual(len(point_logs), 5)
        self.failUnlessEqual(point_logs[0][0].id, 38)
        self.failUnlessEqual(point_logs[0][1], None)
        self.failUnlessEqual(point_logs[1][0].id, 22)
        self.failUnlessEqual(point_logs[1][1].id, point_logs[0][0].id)
        self.failUnlessEqual(point_logs[2][0].id, 7)
        self.failUnlessEqual(point_logs[2][1].id, point_logs[1][0].id)
        self.failUnlessEqual(point_logs[3][0].id, 6)
        self.failUnlessEqual(point_logs[3][1].id, point_logs[2][0].id)
        self.failUnlessEqual(point_logs[4][0].id, 5)
        self.failUnlessEqual(point_logs[4][1].id, point_logs[3][0].id)
        
        # chess stuff
        self.failIf(response.context[0]["chess"]["last_match"] != None)
        
        self.failUnlessEqual(response.context[0]["chess"]["standing"], 2)
        self.failUnlessEqual(response.context[0]["chess"]["matches_played"], 0)
        self.failUnlessEqual(response.context[0]["chess"]["matches_won"], 0)
        self.failUnlessEqual(response.context[0]["chess"]["matches_lost"], 0)
        
        max_points_log = response.context[0]["chess"]["max_points_log"]
        self.failUnlessEqual(1200, max_points_log[0])

        min_points_log = response.context[0]["chess"]["min_points_log"]
        self.failUnlessEqual(1200, min_points_log[0])

        # table football stuff
        self.failIf(response.context[0]["tf"]["last_match"] == None)
        last_match = response.context[0]["tf"]["last_match"]
        self.failUnlessEqual(last_match.id, 5)

        self.failUnlessEqual(response.context[0]["tf"]["standing"], 1)
        self.failUnlessEqual(response.context[0]["tf"]["matches_played"], 3)
        self.failUnlessEqual(response.context[0]["tf"]["matches_won"], 3)
        self.failUnlessEqual(response.context[0]["tf"]["matches_lost"], 0)
        
        max_points_log = response.context[0]["tf"]["max_points_log"]
        self.failUnlessEqual(1260, max_points_log[0])

        min_points_log = response.context[0]["tf"]["min_points_log"]
        self.failUnlessEqual(1200, min_points_log[0])